package com.gy.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.swing.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

//第一步 配置  三大核心要素
@Configuration
public class ShiroConfig {
//ShiroFilterFactoryBean:3  shiro过滤的一些对象
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean
    (@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //关联DefaultWebSecurityManager
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
        //添加Shiro的内置过滤器
        /*
        * anon:无需认证就可以访问
        * authc：必须认证了才能访问
        * user:必须拥有了记住我功能才能用
        * perms:拥有对某个资源的权限才能访问
        * role:拥有某个角色权限才能访问
        * */
        Map<String, String> filterMap=new LinkedHashMap<>();//2

        filterMap.put("/worker/**","perms[user:work]");
        filterMap.put("/admin/**","perms[user:admin]");


        filterMap.put("/dashboard","authc");
        filterMap.put("/baoxiu","authc");
        filterMap.put("/main","authc");
        filterMap.put("/baoxiu/**","authc");
        filterMap.put("/all/**","authc");
        bean.setFilterChainDefinitionMap(filterMap);//1
        //如果没有认证登录  进入登录页面
        bean.setLoginUrl("/index.html");//3
        //设置未授权的请求
        bean.setUnauthorizedUrl("/NoAuthor");
        return bean;
    }


//DafaultWebSecurityManager:2  安全对象
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){

        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联UserRealm
        securityManager.setRealm(userRealm);

        return securityManager;
    }


//创建 realm对象 ，需要自定义1 （在UserRealm中自定义）
    @Bean
    public UserRealm userRealm(){

        return new UserRealm();
    }


    //整合ShiroDialect:用来整合shiro和 thymeleaf
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }










}
